import re
import base64
import requests
import time
from bs4 import BeautifulSoup
import pymongo

AlI_CLOUD_URL = "mongodb://47.98.146.210:27017" # 阿里云地址

cloud_client = pymongo.MongoClient(AlI_CLOUD_URL)
cloud_db = cloud_client["WeChat"]
cloud_zbsj_list = cloud_db["zbsj_list"]
cloud_zbsj_detail = cloud_db["zbsj_detail"]

def main(begin, end):

    id = begin

    list = cloud_zbsj_list.find({"drjId": {"$gt": begin, "$lte": end}}, {"drjId": 1})

    drjId_list = []
    for item in list:
        drjId_list.append(item["drjId"])

    for item in drjId_list:

        id = item

        encodestr = base64.b64encode(str(id).encode('utf-8'))
        url = "http://www.darenji.com/detail/%s.html" % str(encodestr, 'utf-8')
        html = requests.get(url)
        html_text = html.text
        bs = BeautifulSoup(html_text, "html.parser")

        # 主播名称
        anchorName = bs.find("label", {"class": "fs22"})
        anchorName = anchorName.contents[0].string

        if anchorName is not None:
            # 主播id
            anchorAvatar = bs.select_one("div.wxDetail-img img")
            anchorAvatar = anchorAvatar.attrs['src']

            list_one = cloud_zbsj_list.find_one({"drjId": item})
            detail_one = cloud_zbsj_detail.find_one({"drjId": item})

            list_one["anchorAvatar"] = anchorAvatar
            detail_one["anchorAvatar"] = anchorAvatar

            cloud_zbsj_list.update_one({"drjId": item}, {"$set": list_one})
            cloud_zbsj_detail.update_one({"drjId": item}, {"$set": detail_one})

            print("drjId = {} done".format(item))

        else:
            print("{} - {} no data".format(id, url))

        time.sleep(1)

    print('end' + str(id))

if __name__ == "__main__":
    main(190000, 200000)
